package cn.usian.controller;

import cn.usian.pojo.Order;
import cn.usian.pojo.User;
import cn.usian.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
   private RestTemplate restTemplate;

    @GetMapping("{id}")
    public Order findById(@PathVariable Long id){
        //因为在查询的过程中发现user对象为null
        Order order = orderService.findById(id);
        //根据userid获取user对象
        //调用user_Service
        //user_serviced的地址
        //http://localhost:8081/user/1
        //声明请求的url
//        String url = "http://localhost:8081/user/"+order.getUserId();
        String url = "http://userservice/user/"+order.getUserId();
        //远程调用相关的方法
        User user = restTemplate.getForObject(url, User.class);
        //将user对象封装到model中
        order.setUser(user);
        return order;
    }
}
